﻿using GraduationProject.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace GraduationProject.Webs.Areas.Admin.Controllers
{
    public class AdminCommentController : AdminBaseController
    {
        #region 字段
        public readonly GraduationProject.RDBMS.Comment _commentService = new RDBMS.Comment();
        #endregion
        // GET: Admin/AdminComment
        public ActionResult Index(int NovelID)
        {
            ViewData["NovelID"] = NovelID;
            return View();
        }
        /// <summary>
        /// 获取评论内容
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetCommentData(int NovelID)
        {
            Comment[] Comments;
            var UserInfoList = _commentService.TrySelectCommentByNovelID(out Comments, NovelID);
            if (UserInfoList)
            {
                return new NewtonsoftJson(new { code = 0, msg = "成功", count = Comments.ToList().Count, data = Comments });
            }
            return Json(new { code = 1, msg = "抱歉,暂无数据", count = 0, data = new List<Comment>() });
        }
        /// <summary>
        /// 评论删除接口
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AdminDelete(int[] ids)
        {
            if (ids != null)
            {
                bool userResult;
                using (var conn = new RDBMS.Comment())
                {
                    if (conn.TryDeleteComment(out userResult, ids))
                    {
                        if (userResult)
                        {
                            return Json(new { code = 0, msg = "删除成功！" });
                        }
                    }
                }
            }
            return Json(new { code = 1, msg = "删除失败，请稍后重试！" });
        }
        /// <summary>
        /// 评论修改点赞量（管理员功能,默认置零）
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult UpdateAgreeNum(int CommentID, int AgreeNum)
        {
            bool userResult;
            var Result = _commentService.TryUpdateAgreeNum(out userResult, CommentID, AgreeNum);
            if (Result)
            {
                return Json(new { code = 0, msg = "修改成功！" });
            }
            return Json(new { code = 1, msg = "修改失败，请稍后重试！" });
        }
        /// <summary>
        /// 评论修改反对量（管理员功能,默认置零）
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult UpdateDisAgreeNum(int CommentID, int DisAgreeNum)
        {
            bool userResult;
            var Result = _commentService.TryUpdateDisAgreeNum(out userResult, CommentID, DisAgreeNum);
            if (Result)
            {
                return Json(new { code = 0, msg = "修改成功！" });
            }
            return Json(new { code = 1, msg = "修改失败，请稍后重试！" });
        }
    }
}